<?php
	/** 发布出售房源类 
	@author ffpan
	@date 2010-04-10
	@version 1.0
	**/

	class Renthouse{
		function __construct(){
			$this->fangDb = new wbDatabase();
		}

		function __destruct(){}

		function __get($att){
			return $att;
		}

		function __set($att , $val){
			$this->$att = $val;
		}

		function safeVal($val){
			if (!get_magic_quotes_gpc()) {
				 $val = addslashes($val);
			}
			return $val;
		}

		function safePostVal(){
			if (!get_magic_quotes_gpc()) {
			   foreach($_POST as &$v){
				 $v = addslashes($v);
			   }
			}
			return $val;
		}
		
		
		
		//注册中介
		function addOneHouse(){
			$this->safePostVal();
			//$title = $_POST["title"];
			$infoid= $_POST["infoid"];
			$lx= $_POST["lx"];
			$area= $_POST["area"];
			$plaza = $_POST["plaza"];
			$location = $_POST["location"];
			$square = $_POST["square"];
			$style = $_POST["style1"]."/". $_POST["style2"]."/". $_POST["style3"];
			$mstyle = implode("|" , array($_POST["mstyle1"] , $_POST["mstyle2"]));
			$decoration = $_POST["decoration"];
			$floor = $_POST["floor1"]."/".$_POST["floor2"];
			$age = $_POST["age"];
			$price = $_POST["price"];
			$isfull = $_POST["isfull"];
			$detail = $_POST["detail"];
			$ispublic =  $_POST["ispublic"];
			$welcome = $_POST["welcome"];
			$prop = $_POST["prop"];
			$aid = $prop==0?$_POST["publisherid"]:"-1";
			$uid = $prop==0?"-1":$_POST["publisherid"];
			$m_address = $_POST["m_address"];
			$m_name = $_POST["m_name"];
			$m_tel = $_POST["m_tel"];
			$m_detail = $_POST["m_detail"];

			$sql = "insert into ".__table_renthouse__." 
					values(
					'',
					'".$aid."',
					'".$uid."',
					'".$prop."',
					'".$infoid."',
					'".$lx."',
					'".$area."',
					'',
					'".$plaza."',
					'".$location."',
					'".$square."',
					'".$style."',
					'".$mstyle."',
					'".$age."',
					'".$decoration."',
					'".$floor."',
					'".$price."',
					'".$isfull."',
					'".$ispublic."',
					'".$welcome."',
					'".$detail."',
					now() , 
					now() ,
					'".$m_address."',
					'".$m_name."',
					'".$m_tel."',
					'".$m_detail."'
					)";
			$result = $this->fangDb->query($sql);
			if($result){
				if($ispublic=="1"){
					$this->addOnePlaza($plaza);
					if(__user_prop__=="0"){
						$sql  = "update  ".__table_agentlist__." set scores = scores + ".__upload_score__." where id = ".__user_id__;
						$result = $this->fangDb->query($sql);
					}
				}
				return "success";
			}else{
				return "fail";
			}
		}


		//更新一个出租房源
		function updateOneHouse(){
			$this->safePostVal();
			$rid = $_POST["rid"];
			$infoid= $_POST["infoid"];
			$lx= $_POST["lx"];
			$area= $_POST["area"];
			$plaza = $_POST["plaza"];
			$location = $_POST["location"];
			$square = $_POST["square"];
			$style = $_POST["style1"]."/". $_POST["style2"]."/". $_POST["style3"];
			$mstyle = implode("|" , array($_POST["mstyle1"] , $_POST["mstyle2"]));
			$decoration = $_POST["decoration"];
			$floor = $_POST["floor1"]."/".$_POST["floor2"];
			$age = $_POST["age"];
			$price = $_POST["price"];
			$isfull = $_POST["isfull"];
			$detail = $_POST["detail"];
			$ispublic = $_POST["ispublic"];
			$welcome = $_POST["welcome"];
			$m_address = $_POST["m_address"];
			$m_name = $_POST["m_name"];
			$m_tel = $_POST["m_tel"];
			$m_detail = $_POST["m_detail"];

			if(!isset($rid)){return "fail";}
			$sql = "update ".__table_renthouse__." set
					title = '',
					infoid = '".$infoid."',
					lx = '".$lx."' , 
					area = '".$area."',
					plaza = '".$plaza."',
					square = '".$square."',
					style = '".$style."',
					mstyle = '".$mstyle."',
					age = '".$age."',
					decoration = '".$decoration."',
					floor = '".$floor."',
					price = '".$price."',
					isfull = '".$isfull."',
					ispublic = '".$ispublic."',
					welcome = '".$welcome."',
					detail = '".$detail."',
					updatedate = now() ,
					m_address = '".$m_address."',
					m_name = '".$m_name."',
					m_tel = '".$m_tel."',
					m_detail = '".$m_detail."' where id=".$rid;
			$result = $this->fangDb->query($sql);
			if($result){
				return "success";
			}else{
				return "fail";
			}
		}
		
		//删除一个房源
		function deleteOneHouse(){
			$rid = $this->safeVal($_GET["rid"]);
			$sql = "select plaza from ".__table_renthouse__." where id = ".$rid;
			$result = $this->fangDb->query($sql);
			$row = $result->fetch_array();
			$plaza = $row[0];
			$this->deleteOnePlaza($plaza);
			$sql = "delete from ".__table_renthouse__." where id=".$rid;
			$result = $this->fangDb->query($sql);
			if($result){
				return "success";
			}else{
				return "fail";
			}
		}

		function addOnePlaza($plaza){
			$sql = "select * from ".__table_plaza__." where name='".$plaza."'";
			$result = $this->fangDb->query($sql);
			if($result->num_rows==0){
				$sql = "insert into ".__table_plaza__." values ('' , '".$plaza."' , 0 , 0 , '' , 0 , 1)";
			}else{
				$sql = "update ".__table_plaza__." set rnumber = rnumber+1 where name='".$plaza."'";
			}
			$result = $this->fangDb->query($sql);
		}

		function deleteOnePlaza($plaza){
			$sql = "select * from ".__table_plaza__." where name='".$plaza."'";
			$result = $this->fangDb->query($sql);
			if($result->num_rows!=0){
				$sql = "update ".__table_plaza__." set rnumber = rnumber-1 where name='".$plaza."'";
			}
			$result = $this->fangDb->query($sql);
		}


		function showPlaza($type , $pl=""){
			if($type==0){
				$sql = "select * from ".__table_plaza__." where lat=0 or lng=0";
			}else{
				if($pl==""){
					$sql = "select * from ".__table_plaza__." where lat<>0 and lng<>0 and rnumber<>0";
				}else{
					$arr2 = explode("|" , $pl);
					foreach($arr2 as &$v){
						$v = " id = ".$v." ";
					}
					$where = implode(" or " , $arr2);
					$sql = "select * from ".__table_plaza__." where lat<>0 and lng<>0 and rnumber<>0 and (".$where.")";

				}
			}
			if ($result = $this->fangDb->query($sql)) {
			   while ($row = $result->fetch_assoc()){
				    $one = array(
					   "id" => $row["id"], 
					   "name" => $row["name"] , 
					   "lat" => $row["lat"] , 
					   "lng" => $row["lng"] , 
						"address" => $row["address"], 
						"snumber" => $row["snumber"], 
					   "rnumber" => $row["rnumber"]
					);
				    $arr[] = $one;
			   }
			   $result->close();
			}
			return $arr;
		}

		function updatePlaza($pname , $lat , $lng){
			$sql = "update ".__table_plaza__." set lat=".$lat." , lng=".$lng." where name='".$pname."'";
			$result = $this->fangDb->query($sql);
			if($result){
				return "success";
			}else{
				return "fail";
			}
		}
		
		
		//上架|下架某一个出售房源
		function updateListRentHouse($upordown){
			$ids = $_GET["ids"];
			if($upordown==1){
				$arr = explode("," , $ids);
				$num = count($arr) * __upload_score__;
				$sql  = "update  ".__table_agentlist__." set scores = scores + ".$num." where id = ".__user_id__;
				$result = $this->fangDb->query($sql);
			}
			$sql = "update ".__table_renthouse__." set ispublic = ".$upordown." where id in (".$ids.")";
			$result = $this->fangDb->query($sql);
			if($result){
				return "success";
			}else{
				return "fail";
			}
		}
		

	}

	$myRenthouse = new Renthouse();
?>